﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

namespace School.Pages.BOOKS
{
    public partial class SRV_Book_Stock_Record_Equality : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1101) == true)
                {

                    if (!this.Page.IsPostBack)
                    {
                        btnEqual.Visible = false;
                        FillDropList();
                    }
                    lblResult.Text = "";
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
           
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            //CultureInfo SaCulture = new CultureInfo("ar-eg");
            //DateTimeFormatInfo hijriDateInfo = SaCulture.DateTimeFormat;
            //hijriDateInfo.ShortDatePattern = "MM/dd/yyyy";
            //DateTime date = DateTime.Parse(ddlDates.SelectedItem.Text, hijriDateInfo);
            Response.Redirect("~/BOOKS/ReportPrint.aspx?Date=" + ddlDates.SelectedItem.Text);
        }

        private void FillDropList()
        {
            CultureInfo SaCulture = new CultureInfo("ar-eg");
            DateTimeFormatInfo hijriDateInfo = SaCulture.DateTimeFormat;
            hijriDateInfo.ShortDatePattern = "MM/dd/yyyy";
            List<string> lst = new List<string>();
            foreach (var item in School.Service.BOOKS.SRV_BOOKS_TRANSACTIONS.SRV_Books_STOCKTACKINGAll())
            {
                lst.Add(DateTime.Parse(item.Date_Time.ToShortDateString(), hijriDateInfo).ToShortDateString());
            }
            ddlDates.DataSource = lst;
            ddlDates.DataBind();
        }
        
        protected void btnEqual_Click(object sender, EventArgs e)
        {
            CultureInfo SaCulture = new CultureInfo("ar-eg");
            DateTimeFormatInfo hijriDateInfo = SaCulture.DateTimeFormat;
            hijriDateInfo.ShortDatePattern = "MM/dd/yyyy";
            List<string> bookslst = new List<string>();
            foreach (GridViewRow row in gvdata.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    bookslst.Add(row.Cells[0].Text.TrimEnd());
                }
            }
            if (School.Service.BOOKS.SRV_Book_StockTacking.BookEquakity(bookslst, DateTime.Parse(ddlDates.SelectedItem.Text, hijriDateInfo)))
            {
               
                lblResult.Text = "تمت التسويه بنجاح";
                ClearData();
            }
        }
      
        private void ClearData()
        {
            foreach (GridViewRow row in gvdata.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    ((TextBox)row.FindControl("txtQty")).Text = "";
                    ((TextBox)row.FindControl("txtDifferance")).Text = "";
                }
            }
        }

        protected void ddlDates_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlDates.SelectedIndex != 0)
            {

                CultureInfo SaCulture = new CultureInfo("ar-eg");
                DateTimeFormatInfo hijriDateInfo = SaCulture.DateTimeFormat;
                hijriDateInfo.ShortDatePattern = "MM/dd/yyyy";
                IEnumerable<object> lst = School.Service.BOOKS.SRV_BOOKS_TRANSACTIONS.SRV_Books_STOCKTACKINGSearch
                     (DateTime.Parse(ddlDates.SelectedItem.Text, hijriDateInfo));
                gvdata.DataSource = lst;
                gvdata.DataBind();
                GrdPanel.Visible = true;
                btnEqual.Visible = true;
            }
        }
    
    }
}